<template>
  <div class="address-detail-main">
    <!-- 对话框 -->
    <el-dialog
      title="新增地址"
      :visible.sync="isOpen"
      width="40%"
      @close="handleClose"
    >
      <el-form
        ref="form"
        class="detail-form"
        :model="form"
        :rules="rules"
        label-width="120px"
        size="large"
      >
        <!-- 使每两列在一行 -->
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="发货地址" prop="addressName">
              <el-input v-model="form.addressName" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="发货人姓名" prop="nameName">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="发货人手机号" prop="cellphoneName">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="邮政编码" prop="postcodeName">
              <el-input v-model="form.postcode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="收货地址">
              <el-cascader
                v-model="form.cityCode"
                :options="options"
                size="large"
                clearable
                filterable
                :show-all-levels="false"
                @change="handleChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="详细地址">
              <el-input v-model="form.detailAddress" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import addressApi from '@/api/order/address'
import { regionDataPlus, CodeToText } from 'element-china-area-data'
import mixin from '@/mixins/index'
export default {
  name: 'AddressDetai',
  mixins: [mixin],
  data() {
    return {
      options: regionDataPlus,
      isOpen: false,
      address: [],
      rules: {
        /* 校验 */
        addressName: [{ required: true, message: '请填写发货地址', trigger: 'blur' }],
        nameName: [{ required: true, message: '请填写发货人姓名', trigger: 'blur' }],
        cellphoneName: [{ required: true, message: '请填写发货人手机号', trigger: 'blur' }],
        postcodeName: [{ required: true, message: '请填写邮政编码', trigger: 'blur' }]
      },
      form: {
        addressName: '', // 地址名称
        name: '', // 发货人姓名
        phone: '',
        postCode: '', // 邮政编码
        cityCode: [], // 地址编码
        detailAddress: '', // 详细收货地址
        receiveStatus: 0,
        sendStatus: 0
      },
      id: ''
    }
  },
  methods: {
    openAddressDetail(id) {
      if (id) {
        this.id = id
        this.getAddressDetail()
      }
      this.isOpen = true
    },
    getAddressDetail() {
      addressApi.addressDetail(this.id).then(res => {
        if (res.success) {
          this.form = {
            ...res.data.address,
            cityCode: res.data.address.cityCode.split(',')
          }
        }
      })
    },
    handleChange(v) {
      /* this.address.province = CodeToText[value[0]]
      this.address.city = CodeToText[value[1]]
      this.address.region = CodeToText[value[2]]
      this.address.cityCode = value.join(',') */
      this.cityCode = v
    },
    goAddress() {
      this.isOpen = true
    },
    handleClose() {
      this.isOpen = false
    },
    onSubmit() {
      // 发ajax请求
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.id) {
            const data = {
              ...this.form,
              cityCode: this.form.cityCode.join(','),
              province: CodeToText[this.cityCode[0]],
              city: CodeToText[this.cityCode[1]],
              region: CodeToText[this.cityCode[2]]
            }
            addressApi.updateAddress(data).then(res => {
              if (res.success) {
                this.isOpen = false
                this.$emit('success')
              } else {
                this.$message(res.message)
              }
            })
          } else {
            const data = {
              ...this.form,
              cityCode: this.form.cityCode.join(','),
              province: CodeToText[this.cityCode[0]],
              city: CodeToText[this.cityCode[1]],
              region: CodeToText[this.cityCode[2]]
            }
            addressApi.saveAddress(data).then(res => {
              if (res.success) {
                this.isOpen = false
                this.$emit('success')
              } else {
                this.$message(res.message)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.address-detail-main{
  .margin-30{
    margin: 30px;
  }
}
</style>
